package no.monsen.client.timer;

import no.monsen.client.common.exception.MonsenException;
import no.monsen.client.common.logging.Logger;
import no.monsen.client.controller.MonsenMessageList;
import no.monsen.monsenservice.MonsenFact;
import no.monsen.service.MonsenService;

import org.apache.commons.logging.Log;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

/**
 * 
 * @author Marius Breivik Created: 21.59.39 / 10. mars. 2008
 */
public class MonsenFactJob extends QuartzJobBean {
	@Logger
	private Log log;
	
	private MonsenService monsenService;
	private MonsenMessageList messageList;
	
	@Override
	protected void executeInternal(JobExecutionContext jectx) throws JobExecutionException {
		try {
			messageList.postMessage(monsenService.getMonsenFact(), "Info");
		} catch (MonsenException e) {
			log.error("Failed to retrieve fact from monsen-service " + e);
			MonsenFact monsenFact = new MonsenFact();
			monsenFact.setFact(e.getMessage());
			messageList.postMessage(monsenFact, "Error");
		} catch (Exception e) {
			log.error("Failed to retrieve fact from monsen-service " + e);
			MonsenFact monsenFact = new MonsenFact();
			monsenFact.setFact(e.getMessage());
			messageList.postMessage(monsenFact, "Error");
		}
	}
	
	public void setMessageList(MonsenMessageList messageList) {
		this.messageList = messageList;
	}

	public void setMonsenService(MonsenService monsenService) {
		this.monsenService = monsenService;
	}
}
